package com.wowkids.controller;

import com.wowkids.common.JWTUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author DoDayum
 * @Date 2024-02-26-20:52
 */
@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(ObjectUtils.isEmpty(cookies)){
            return "login";
        }
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())) {

                try {
                    Map<String, String> decode = JWTUtil.decode(cookie.getValue());
                    String user = decode.get("user");
                    model.addAttribute("message", user);
                    return "index";
                }catch (Exception e){
                    return "login";
                }
            }
        }
        return "login";
    }
}
